﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using Core.Contract.DataContract;
using Core.DAO;
using Data.BO;
using www.Models;

namespace www.Controllerss
{
   public class StoryController
   {
       #region value
       private readonly StoryRefBO _storyBo = new StoryRefBO();
       private readonly StoryValueBO _storyValueBo =new StoryValueBO();
       #endregion

       #region ham lay du lieu

       public IQueryable<RelaxStoryDataContract> GetStoryDataContracts()
       {
           using (var scope = new TransactionScope())
           {
               var lReturn = _storyBo.GetStoryDataContracts();
               scope.Complete();
               return lReturn;
           }
       }
       public IQueryable<RelaxStoryDataContract> GetStoryDataContract()
       {
           using (var scope = new TransactionScope())
           {
               var lReturn= _storyBo.GetStoryDataContract();
               scope.Complete();
               return lReturn;
           }
       }
       #endregion

       #region Add
       public long Add(List<RelaxStoryDataContract> storyDataContract)
       {
           using (var scope = new TransactionScope())
           {
               var iD = 0;
               var story = new NEWS_REF
               {
                   date = storyDataContract[0].Date,
                   priority = storyDataContract[0].Priority,
                   image = storyDataContract[0].Image,
                   categoryId = Convert.ToInt32(storyDataContract[0].CategoryId),
                   isdefault = storyDataContract[0].IsDefault,
                   active = storyDataContract[0].Active,
                   userId = storyDataContract[0].Userid,
                   userbrower = storyDataContract[0].UserBrower,
                   report = storyDataContract[0].Report,
                   view = storyDataContract[0].View
               };
               _storyBo.Add(story);
               var first = _storyBo.GetStoryId().OrderByDescending(o => o.Id).FirstOrDefault();
               if (first != null)
               {
                   iD = Convert.ToInt32(first.Id);
                   foreach (var item in storyDataContract)
                   {
                       var newValue = new NEWS_VALUE
                       {
                           name = item.Name,
                           tag = item.Tag,
                           keyword = item.Keyword,
                           decription = item.Decription,
                           detail = item.Detail,
                           languagecode = item.Languagecode,
                           newid = iD
                       };
                       _storyValueBo.Add(newValue);
                   }
               }
                   
               scope.Complete();
               return iD;
           }
       }
       #endregion

       #region Update
       public Boolean Update(List<RelaxStoryDataContract> storyDataContract)
       {
           using (var scope = new TransactionScope())
           {
             
               var story = new NEWS_REF
               {
                   id = Convert.ToInt32(storyDataContract[0].Id),
                   date = storyDataContract[0].Date,
                   priority = storyDataContract[0].Priority,
                   categoryId = Convert.ToInt32(storyDataContract[0].CategoryId),
                   isdefault = storyDataContract[0].IsDefault,
                   active = storyDataContract[0].Active,
                   image = storyDataContract[0].Image,
                   userId = storyDataContract[0].Userid,
                   userbrower = storyDataContract[0].UserBrower,
                   report = storyDataContract[0].Report,
                   view = storyDataContract[0].View
               };
               _storyBo.Update(story);
               foreach (var item in storyDataContract)
               {
                   var newValue = new NEWS_VALUE
                   {
                       newid = Convert.ToInt32(item.Id),
                       languagecode = item.Languagecode,
                       name = item.Name,
                       tag = item.Tag,
                       keyword = item.Keyword,
                       decription = item.Decription,
                       detail = item.Detail
                   };
                   if (!_storyValueBo.Update(newValue))
                   {
                       _storyValueBo.Add(newValue);
                   }
               }
               scope.Complete();
               return true;
           }
       }

       public void UpdateView(long id)
       {
           using (var scope = new TransactionScope())
           {
               _storyBo.UpdateView(id);
               scope.Complete();
           }
          
       }
       #endregion

       #region Delete

       public Boolean Delete(List<long> lId, long iLanguageCode)
       {
           using (var scope = new TransactionScope())
           {
               foreach (var id in lId)
               {
                   _storyValueBo.Delete(id,iLanguageCode);
               }
               scope.Complete();
               return true;
           }
       }
       #endregion
   }
}
